﻿using FRMS.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Threading.Tasks;

namespace FRMS.WebSockets
{
    public class WebSocketManage
    {
        WebSocket _socket;

        public WebSocket Socket
        {
            get
            {
                return _socket;
            }
            set
            {
                _socket = value;
            }
        }

        public async Task SendMessage(string Message)
        {
            if (_socket != null)
            {
                byte[] bMessage = System.Text.Encoding.UTF8.GetBytes(Message);
                await _socket.SendAsync(new ArraySegment<byte>(bMessage), WebSocketMessageType.Text, true, new System.Threading.CancellationToken());
            }
        }

        public async Task<bool> PushMessage(HistoryCount history)
        {
            string Msg = JsonConvert.SerializeObject(history);
            await SendMessage(Msg);
            return false;
        }
    }
}
